''' Game Menu imlementation'''
import sys,os
import soya
import soya.pudding as pudding
import time

from consts import *

class MenuOption(pudding.control.Label):

    def __init__(self, label, function=None,param=None ):
        pudding.control.Label.__init__(self, label = label)
        self.function = function
        self.param = param
        self.color1 = (1., 1., 0., 1.)
        self.color2 = (0.5, 1., 0.2, 1.)
        self.color=self.color1
    
        
    def on_focus(self):
        self.color = self.color2
        self.parent.select_option(self)
    def on_loose_focus(self):
        self.color = self.color1

    def on_mouse_up(self, button, x, y):
        print "click on %s" % self.label
        
        #just for testing purposes
        if self.label=="Exit":sys.exit()
        #just for testing purposes
        
        
        if self.function:
            if self.param:self.function(self.param)
            else:self.function()
        return True


class MenuContainer(pudding.container.VerticalContainer):
    
    def __init__(self,parent,name,**kwargs):
        pudding.container.VerticalContainer.__init__(self,parent,**kwargs)
        self.name=name
        self.active_element=1
        
    def select_option(self,element):
        for elem in self.children:
            if not elem==element:
                elem.focus=False
        self.active_element=self.children.index(element)
        
    def select_next(self):
        if not self.active_element+1>len(self.children)-1:
            self.active_element+=1
            self.children[self.active_element].focus=True
            
    def select_prev(self):
        if not (self.active_element-1<1):
            self.active_element-=1
            self.children[self.active_element].focus=True
            
    def on_show(self):
        self.active_element=1
        self.children[self.active_element].focus=True

        
class GameMenu(pudding.control.Panel):
    
    def __init__(self,parent,label,**kwargs):
        print "creating GameMenu()"
        pudding.control.Panel.__init__(self,parent=parent,label=label,**kwargs)
        self.m_list=[]
        
        
        self.next_level=None
        self.prev_level=None
        
        
   
    def change_menu(self,menu):
        print "Change menu:",menu
        for m in self.m_list:
            if m.name == menu:
                print "OK I got it"
                self.child=self.m_list[self.m_list.index(m)]
                self.label=menu
                self.on_resize()
    
    def create_menus(self):
        m_main=MenuContainer(self,'Main Menu')
        m_sound=MenuContainer(self,'Sound Options')
        m_graph=MenuContainer(self,'Graphics Options')
        m_game=MenuContainer(self,'Game Settings')
        
        ##------------------    Main Menu
        ##create menus contents
        m_main.add_child((pudding.control.Label(label='')),pudding.CENTER_HORIZ)
        m_main.add_child((MenuOption(label='Back To Game',function=self.next_level,param=None)), pudding.CENTER_HORIZ)
        m_main.add_child((MenuOption(label='Next Level',function=self.next_level,param=None)), pudding.CENTER_HORIZ)
        m_main.add_child((MenuOption(label='Previous Level',function=None,param=None)), pudding.CENTER_HORIZ)
        m_main.add_child((MenuOption(label='Sound Options',function=self.change_menu,param='Sound Options')), pudding.CENTER_HORIZ)
        m_main.add_child((MenuOption(label='Graphics Options',function=self.change_menu,param='Graphics Options')), pudding.CENTER_HORIZ)
        m_main.add_child((MenuOption(label='Game Settings',function=self.change_menu,param='Game Settings')), pudding.CENTER_HORIZ)
        m_main.add_child((MenuOption(label='Quit',function=sys.exit,param=None)), pudding.CENTER_HORIZ)
        
        ##------------------    Sound Menu
        ##add empty entry
        m_sound.add_child((pudding.control.Label(label='')),pudding.CENTER_HORIZ)
        m_sound.add_child((MenuOption(label='Music On/Off',function=None,param=None)), pudding.CENTER_HORIZ)
        m_sound.add_child((MenuOption(label='Sound On/Off',function=None,param=None)), pudding.CENTER_HORIZ)
        m_sound.add_child((MenuOption(label='Skip Track',function=None,param=None)), pudding.CENTER_HORIZ)
        m_sound.add_child((MenuOption(label='Enter tracks localization',function=None,param=None)), pudding.CENTER_HORIZ)
        m_sound.add_child((MenuOption(label='Go Back',function=self.change_menu,param='Main Menu')), pudding.CENTER_HORIZ)
        
        ##------------------    Graphics Menu
        m_graph.add_child((pudding.control.Label(label='')),pudding.CENTER_HORIZ)
        m_graph.add_child((MenuOption(label='Switch Fullscreen',function=None,param=None)), pudding.CENTER_HORIZ)
        m_graph.add_child((MenuOption(label='Stitch Dynamic Lights',function=None,param=None)), pudding.CENTER_HORIZ)
        m_graph.add_child((MenuOption(label='Go Back',function=self.change_menu,param='Main Menu')), pudding.CENTER_HORIZ)
        
        ##-------------------   Game Menu
        m_game.add_child((pudding.control.Label(label='')),pudding.CENTER_HORIZ)
        m_game.add_child((MenuOption(label='Some Settings',function=None,param=None)), pudding.CENTER_HORIZ)
        m_game.add_child((MenuOption(label='Some Settings',function=None,param=None)), pudding.CENTER_HORIZ)
        m_game.add_child((MenuOption(label='Go Back',function=self.change_menu,param='Main Menu')), pudding.CENTER_HORIZ)
        
        #create menu list
        self.m_list.append(m_main)
        self.m_list.append(m_sound)
        self.m_list.append(m_graph)
        self.m_list.append(m_game)
        
        for m in self.m_list:
            m.right=0
            m.padding=25
        self.child=self.m_list[0]
        #print self.m_list
        
    def on_show(self):
        self.child.visible=True
        self.change_menu('Main Menu')
        pass
    
    def advance_time(self,proportion):
        pudding.control.Panel.advance_time(self,proportion)
        self.left=(soya.get_screen_width()-self.width)/2
        self.height=self.child.padding*2*len(self.child.children)
        self.top=(soya.get_screen_height()/len(self.child.children))
        pass

    
class GameUI:
    
    def __init__(self,parent=None):
        self.visible=0	
        self.parent=parent
        self.widget=pudding.core.RootWidget(width=640, height=480)
        
    	self.menu_panel=GameMenu(self.widget,label="Menu",top=25,bottom=25,width=200,height=300)
        
        self.menu_panel.background_color = ( 0.5,0.6,0.3,0.7)
        self.menu_panel.visible=0
        
    def show(self,st):
        if st in [0,1]:
            if not self.menu_panel.visible==st:
                for element in self.menu_panel.child.children:
                    element.on_loose_focus()
                self.menu_panel.visible=st
                        
    def action_handler(self,action):
        #print "GameUI.action_handler, action:",action
        if action==MENU_ENTER:
            self.menu_panel.child.children[self.menu_panel.child.active_element].on_mouse_up(None,None,None)
        if action==ACT_BACKWARD:
            self.menu_panel.child.select_next()
        if action==ACT_FORWARD:
            self.menu_panel.child.select_prev()        
        #clear keyboard events buffer
        self.parent.ctrl.clear()

        
if __name__=="__main__":
    soya.init("Duupa",create_surface=1)
    try:
        pudding.init()
    except TypeError:
        pass
    
    m=GameUI(soya.World())
    camera=soya.Camera(m.parent)
    #camera.z=3.0
    m.widget.add_child(camera)
    soya.set_root_widget(m.widget)        
    m.show(1)
    
    while True:
        for event in soya.process_event():
            if event[0]==soya.sdlconst.KEYDOWN:
                #print m.menu_panel.left
                soya.quit()
                sys.exit()
        
        pudding.main_loop.MainLoop(m.parent).main_loop()
        
        
    
    
    
